﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ThreeDPrinting.ORM;

namespace ThreeDPrinting.Pages.Share
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["memberID"] == null)
                Response.Redirect("~/Pages/LogIn");
        }

        protected void btnUpload_Click(object sender, EventArgs e)
        {
            
            if (FileUploadControl.HasFile)
            {
                try
                {
                    using (DBEntities entities = new DBEntities())
                    {

                        UploadedProduct mem = new UploadedProduct();
                        mem.FileName = txtItemName.Text;
                        mem.FileDescription = txtItemDescription.Text;
                        mem.UploadedDate = DateTime.Now;
                        mem.UploadedFileLength = FileUploadControl.PostedFile.ContentLength;                       
                        mem.UploadedFileType = FileUploadControl.PostedFile.ContentType;
                        HttpPostedFile uploadedImage = FileUploadControl.PostedFile;
                        byte[] imageBytes = new byte[FileUploadControl.PostedFile.InputStream.Length];
                        uploadedImage.InputStream.Read(imageBytes, 0, (int)FileUploadControl.PostedFile.ContentLength);
                        mem.UploadedFile = imageBytes;
                        mem.MemberID = Convert.ToInt16(Session["memberID"]);
                        entities.UploadedProducts.Add(mem);
                        //Save the changes to the DB
                        entities.SaveChanges();
                        StatusLabel.Text = "Upload status: File uploaded!";
                    }
                }
                catch (Exception ex)
                {
                    StatusLabel.Text = "Upload status: The file could not be uploaded. The following error occured: " + ex.Message;
                }
            }
        }
    }
}